<script setup>
import {
  Lock,User
} from '@element-plus/icons-vue'
import {ref} from 'vue'
import {login} from '@/api/bookLogin.js'
import { useRouter } from 'vue-router'
import Cookies from 'js-cookie'
const router=useRouter()
const form=ref()
const modelForm=ref({
    username:'张三',
    password:'123'
})
const rules={
    username: [
    { required: true, message: '用户名不能为空' },
  ],
  password:[
    { required: true, message: '密码不能为空' },
    { min: 2, max: 15, message: '密码长度只能2到10', trigger: 'blur' },
  ]
}
// 登录按钮
const formLogin=async()=>{
    await form.value.validate()
    Cookies.set('admin',JSON.stringify(modelForm.value))
    const res=await login(modelForm.value)
    ElMessage.success('登录成功')
    router.push('/index')
}
</script>
<template>
  <el-row>
    <el-col :span="24" class="loginLayout">
        <el-form 
        class="loginFrom" 
        :rules="rules"
        :model="modelForm"
        ref="form"
        >
            <el-form-item>
                <el-text type="primary">
                    <div>
                        <h1>登录</h1>
                    </div>
                </el-text>
            </el-form-item>
            <el-form-item style="width: 400px;" prop="username">
                <el-input  :prefix-icon="Lock"  placeholder="请输入用户名" v-model="modelForm.username"></el-input>
            </el-form-item>
            <el-form-item style="width: 400px;" prop="password">
                <el-input  :prefix-icon="User" placeholder="请输入密码"  v-model="modelForm.password"></el-input>
            </el-form-item>
            <el-form-item >
                <el-button style="width: 400px; height: 40px" type="primary" @click="formLogin">登录</el-button>
            </el-form-item>
            <el-form-item style="width: 400px;">
                <el-link type="primary" @click="test">注册</el-link>
            </el-form-item>
        </el-form>
    </el-col>
  </el-row>

</template>



<style>
.loginLayout{
    height: 100vh;
    background-color: rgb(213, 226, 226);
    display: flex;
    justify-content: center;
    align-items: center;
}
.loginFrom{
    width: 500px;
    height: 400px;
    background-color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}
</style>